package me.hyman.testcustomview.weibo_sport;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import me.hyman.testcustomview.R;
import me.hyman.testcustomview.util.Logger;

/**
 * Created by Hyman on 2016/9/12.
 */
public class WeiboSportsView extends View {

    private int textColor;
    private int textSize;
    private int innerCircleColor;
    private int outerCircleColor;

    private Paint mPaint;
    private Paint mInnerCirclePaint;
    private Rect mBound; //绘制文本的范围

    private float mStartAngle;
    private float mSweepAngle;
    private float mCurrentAngle;

    private int mCurrentPercent;
    private int mTargetPercent;

    public WeiboSportsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WeiboSportsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.WeiboSportsView, defStyleAttr, 0);
        int count = array.getIndexCount();

        for(int i=0; i<count; i++) {
            int attr = array.getIndex(i);
            switch (attr) {
                case R.styleable.WeiboSportsView_titleSize:
                    textSize = array.getDimensionPixelSize(attr, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
                    break;

                case R.styleable.WeiboSportsView_titleColor:
                    textColor = array.getColor(attr, Color.BLACK);
                    break;

                case R.styleable.WeiboSportsView_innerCircleColor:
                    innerCircleColor = array.getColor(attr, Color.BLACK);
                    break;

                case R.styleable.WeiboSportsView_outerCircleColor:
                    outerCircleColor = array.getColor(attr, Color.BLACK);
                    break;
            }
        }

        array.recycle();
        init();
    }

    private void init() {

        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        mInnerCirclePaint = new Paint();
        mInnerCirclePaint.setAntiAlias(true);

        mBound = new Rect();

        mStartAngle = -90;
        mSweepAngle = 360/100;
        mCurrentAngle = 0;

        mTargetPercent = 100;
        mCurrentPercent = 0;
    }

    public void setTargetPercent(int targetPercent) {
        this.mTargetPercent = targetPercent;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width, height;

        Logger.e("widthMode: " + widthMode + ", widthSize: " + widthSize + ", heightMode: "
                + heightMode + ", heightSize: " + heightSize);

        //如果布局里面设置的是固定值,这里取布局里面的固定值;如果设置的是match_parent,则取父布局的大小
        if(widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {

            //如果布局里面没有设置固定值,这里取布局宽度的1/2
            width = widthSize * 1 / 2;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = heightSize * 1 / 2;
        }

        Logger.e("width: " + width + ", height: " + height);

        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //mPaint.reset();

        //设置外圆的颜色
        mPaint.setColor(outerCircleColor);
        //设置外圆为空心
        mPaint.setStyle(Paint.Style.STROKE);

        canvas.drawCircle(getWidth()/2, getWidth()/2, getWidth()/2, mPaint);

        mPaint.setColor(textColor);
        mPaint.setTextSize(textSize);
        String text = String.valueOf(mCurrentPercent);
        //mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.getTextBounds(text, 0, text.length(), mBound);

        canvas.drawText(text, getWidth()/2-mBound.width()/2, getWidth()/2+mBound.height()/2, mPaint);

        mPaint.setTextSize(textSize / 3);
        canvas.drawText("分", getWidth()/2+mBound.width()*3/5, getWidth()/2-mBound.height()*2/5, mPaint);

        mInnerCirclePaint.setColor(innerCircleColor);
        mInnerCirclePaint.setStyle(Paint.Style.STROKE);
        mInnerCirclePaint.setStrokeWidth(20);

        RectF rectF = new RectF(20, 20, getWidth()-20, getWidth()-20);
        canvas.drawArc(rectF, mStartAngle, mCurrentAngle, false, mInnerCirclePaint);

        //判断当前百分比是否小于设置目标的百分比
        if (mCurrentPercent < mTargetPercent) {
            //当前百分比+1
            mCurrentPercent += 1;
            //当前角度+360
            mCurrentAngle += 3.6;
            //每100ms重画一次
            postInvalidateDelayed(100);
        }
    }
}
